/*Copyright (C) 2009 Careil Baptiste

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#ifdef WIN32
	#include <windows.h>
	COORD cursorPos;
	//fonctions spécifiques à WIN32
	void Cursor_GetPos(COORD* pos)
	{
		HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
		CONSOLE_SCREEN_BUFFER_INFO csbi;
		GetConsoleScreenBufferInfo( handle, &csbi);
		*pos = csbi.dwCursorPosition;
	}
	
	void Cursor_SetPos(COORD pos)
	{
		HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
		SetConsoleCursorPosition(handle, pos);
	}
#else
	#include <stdio.h>
#endif

#include "dll.h"
#include "Cursor.h"

///////////////////////////////////////////////////////////
/// Permet de placer le curseur en début de ligne.
///////////////////////////////////////////////////////////
void DLL_IMPEXP CSLP_GotoBeginOfLine()
{
	#ifdef WIN32
	COORD _cursorPos;
	Cursor_GetPos(&_cursorPos);
	_cursorPos.X = 0;
	Cursor_SetPos(_cursorPos);
	#else
	printf("\033[1G");
	#endif
}

///////////////////////////////////////////////////////////
/// Permet de placer le curseur à la ligne précédente
///////////////////////////////////////////////////////////
void DLL_IMPEXP CSLP_GotoPreviousLine()
{
	#ifdef WIN32
	COORD _cursorPos;
	Cursor_GetPos(&_cursorPos);
	_cursorPos.Y--;
	Cursor_SetPos(_cursorPos);
	#else
	printf("\033[1F");
	#endif
}

///////////////////////////////////////////////////////////
/// Permet de placer le curseur à la ligne suivante
///////////////////////////////////////////////////////////
void DLL_IMPEXP CSLP_GotoNextLine()
{
	#ifdef WIN32
	COORD _cursorPos;
	Cursor_GetPos(&_cursorPos);
	_cursorPos.Y++;
	Cursor_SetPos(_cursorPos);
	#else
	printf("\033[1E");
	#endif
}

///////////////////////////////////////////////////////////
/// Permet de placer le curseur aux coordonnées voulus
///////////////////////////////////////////////////////////
void DLL_IMPEXP CSLP_Goto(int r, int c)
{
	#ifdef WIN32
	COORD newCurPos;
	newCurPos.X = c;
	newCurPos.Y = r;
	Cursor_SetPos(newCurPos);
	#else
	char str[64];
	sprintf(str, "\033[%i;%iH", r, c);
	fputs(str, stdout);
	#endif
}

///////////////////////////////////////////////////////////
/// Permet de déplacer le curseur dans la direction voulue
///////////////////////////////////////////////////////////
void DLL_IMPEXP CSLP_MoveCursor(CSLP_WAY way, unsigned int value)
{
	if (value != 0)
	{
		#ifdef WIN32
		COORD newCursorPos;
		Cursor_GetPos(&newCursorPos);
		switch (way)
		{
			case FORWARD:
				newCursorPos.X += value; break;
			case BACKWARD:
				newCursorPos.X -= value; break;
			case DOWN:
				newCursorPos.Y -= value; break;
			case UP:
				newCursorPos.Y += value; break;
			default:
				perror("CSLP_MoveCursor : argument way invalid ");
				return;
		}
		Cursor_SetPos(newCursorPos);
		#else
		char str[64];
		switch (way)
		{
			case FORWARD:
				sprintf(str, "\033[%iC", value); break;
			case BACKWARD:
				sprintf(str, "\033[%iD", value); break;
			case DOWN:
				sprintf(str, "\033[%iB", value); break;
			case UP:
				sprintf(str, "\033[%iA", value); break;
			default:
				fputs("CSLP_MoveCursor : argument way invalid.\n", stderr);
				return;
		}
		fputs(str, stdout);
		#endif
	} // : if (value != 0)
}

///////////////////////////////////////////////////////////
/// Permet de sauvegarder la position courrente du curseur
///////////////////////////////////////////////////////////
void DLL_IMPEXP CSLP_SavePos()
{
	#ifdef WIN32
	Cursor_GetPos(&cursorPos);
	#else
	printf("\033[s");
	#endif
}

///////////////////////////////////////////////////////////
/// Permet de restaurer la position du curseur
///////////////////////////////////////////////////////////
void DLL_IMPEXP CSLP_RestorePos()
{
	#ifdef WIN32
	Cursor_SetPos(cursorPos);
	#else
	printf("\033[u");
	#endif
}

